<?php 
	   class WGToChina
    {
        public function __construct()
        {
        }

        public static function yj_sin2($x)
        {
            //double tt;
            //double ss;
            //int ff;
            //double s2;
            //int cc;
            $ff = 0;
            if ($x < 0)
            {
                $x = -$x;
                $ff = 1;
            }
            $cc = round($x / 6.28318530717959);
            $tt = $x - $cc * 6.28318530717959;
            if ($tt > 3.1415926535897932)
            {
                $tt = $tt - 3.1415926535897932;
                if ($ff == 1)
                    $ff = 0;
                else if ($ff == 0)
                    $ff = 1;
            }
            $x = $tt;
            $ss = $x;
            $s2 = $x;
            $tt = $tt * $tt;
            $s2 = $s2 * $tt;
            $ss = $ss - $s2 * 0.166666666666667;
            $s2 = $s2 * $tt;
            $ss = $ss + $s2 * 8.33333333333333E-03;
            $s2 = $s2 * $tt;
            $ss = $ss - $s2 * 1.98412698412698E-04;
            $s2 = $s2 * $tt;
            $ss = $ss + $s2 * 2.75573192239859E-06;
            $s2 = $s2 * $tt;
            $ss = $ss - $s2 * 2.50521083854417E-08;
            if ($ff == 1)
                $ss = -$ss; 
            return $ss;
        }
        static function Transform_yj5( $x, $y)
        {
            //double tt;
            $tt = 300 + 1 * $x + 2 * $y + 0.1 * $x * $x + 0.1 * $x * $y + 0.1 * sqrt(sqrt($x * $x));
            $tt = $tt + (20 * self::yj_sin2(18.849555921538764 * $x) + 20 * self::yj_sin2(6.283185307179588 * $x)) * 0.6667;
            $tt = $tt + (20 * self::yj_sin2(3.141592653589794 * $x) + 40 * self::yj_sin2(1.047197551196598 * $x)) * 0.6667;
            $tt = $tt + (150 * self::yj_sin2(0.2617993877991495 * $x) + 300 * self::yj_sin2(0.1047197551196598 * $x)) * 0.6667;
            return $tt;
        }
        static function Transform_yjy5($x, $y)
        {
            //double tt;
            $tt = -100 + 2 * $x + 3 * $y + 0.2 * $y * $y + 0.1 * $x * $y + 0.2 * sqrt(sqrt($x * $x));
            $tt = $tt + (20 * self::yj_sin2(18.849555921538764 * $x) + 20 * self::yj_sin2(6.283185307179588 * $x)) * 0.6667;
            $tt = $tt + (20 * self::yj_sin2(3.141592653589794 * $y) + 40 * self::yj_sin2(1.047197551196598 * $y)) * 0.6667;
            $tt = $tt + (160 * self::yj_sin2(0.2617993877991495 * $y) + 320 * self::yj_sin2(0.1047197551196598 * $y)) * 0.6667;
            return $tt;
        }
        static function Transform_jy5($x, $xx)
        {
            //double n;
            //double a;
            //double e;
            $a = 6378245;
            $e = 0.00669342;
            $n = sqrt(1 - e * self::yj_sin2($x * 0.0174532925199433) * self::yj_sin2($x * 0.0174532925199433));
            $n = ($xx * 180) / ($a / $n * cos($x * 0.0174532925199433) * 3.1415926);
            return $n;
        }
        static function Transform_jyj5($x, $yy)
        {
            //double m;
            //double a;
            //double e;
            //double mm;
            $a = 6378245;
            $e = 0.00669342;
            $mm = 1 - $e * self::yj_sin2($x * 0.0174532925199433) * self::yj_sin2($x * 0.0174532925199433);
            $m = ($a * (1 - $e)) / ($mm * sqrt($mm));
            return ($yy * 180) / ($m * 3.1415926);
        }
        static function r_yj()
        {
            //int casm_a;
            //int casm_c;
            $casm_a = 314159269;
            $casm_c = 453806245;
            return 0;
        }
        static function random_yj($casm_rr)
        {
            $result = 0.00;
            //int t;
            //int casm_a;
            //int casm_c;
            $casm_a = 314159269;
            $casm_c = 453806245;
            $result = $casm_a * $casm_rr + $casm_c;
            $t = round($result / 2);
            //int.TryParse((result / 2).ToString("##"), out t);
            $result = $result - $t * 2;
            $result = $result / 2;
            return $result;
        }
        static function IniCasm($w_time, $w_lng, $w_lat, &$casm_rr,
            &$casm_t1, &$casm_t2, &$casm_x1, &$casm_y1, &$casm_x2,
            $casm_y2, $casm_f)
        {
            //int tt;
            //casm_t1 = w_time;
            //casm_t2 = w_time;
            $tt = round($w_time / 0.357);
            //int.TryParse((w_time / 0.357).ToString("##"), out tt);
            $casm_rr = $w_time - $tt * 0.357;
            if ($w_time == 0)
                $casm_rr = 0.3;
            $casm_x1 = $w_lng;
            $casm_y1 = $w_lat;
            $casm_x2 = $w_lng;
            $casm_y2 = $w_lat;
            $casm_f = 3;
        }

        static function wgtochina_lb( $wg_flag, $wg_lng, $wg_lat, $wg_heit, $wg_week, $wg_time, &$china_lng, &$china_lat)
        {
        	 //double x_add;
            //double y_add;
            //double h_add;
            //double x_l;
            //double y_l;
            //double casm_v;
            //double t1_t2;
            //double x1_x2;
            //double y1_y2;
            $casm_rr = 0.00;
            $casm_t1 = 0;
            $casm_t2 = 0;
            $casm_x1 = 0.00;
            $casm_y1 = 0.00;
            $casm_x2 = 0.00;
            $casm_y2 = 0.00;
            $casm_f = 0.00;

            if ($wg_heit > 5000)
            {
                $china_lng = 0;
                $china_lat = 0;
                return 1;
            }
            $x_l = $wg_lng;
            $x_l = $x_l / 3686400.0;
            $y_l = $wg_lat;
            $y_l = $y_l / 3686400.0;
            if ($x_l < 72.004)
            {
                $china_lng = 0;
                $china_lat = 0;
                return 1;
            }
            if ($x_l > 137.8347)
            {
                $china_lng = 0;
                $china_lat = 0;
                return 1;
            }
            if ($y_l < 0.8293)
            {
                $china_lng = 0;
                $china_lat = 0;
                return 1;
            }
            if ($y_l > 55.8271)
            {
                $china_lng = 0;
                $china_lat = 0;
                return 1;
            }
            if ($wg_flag == 0)
            {
                self::IniCasm($wg_time, $wg_lng, $wg_lat, &$casm_rr, &$casm_t1, &$casm_t2, &$casm_x1, &$casm_y1, &$casm_x2, &$casm_y2, &$casm_f);
                $china_lng = $wg_lng;
                $china_lat = $wg_lat;
                return 0;
            }

            $casm_t2 = $wg_time;
            $t1_t2 = (double)($casm_t2 - $casm_t1) / 1000.0;

            if ($t1_t2 <= 0)
            {
                $casm_t1 = $casm_t2;
                $casm_f = $casm_f + 1;
                $casm_x1 = $casm_x2;
                $casm_f = $casm_f + 1;
                $casm_y1 = $casm_y2;
                $casm_f = $casm_f + 1;
            }
            else
            {
                if ($t1_t2 > 120)
                {
                    if ($casm_f == 3)
                    {
                        $casm_f = 0;
                        $casm_x2 = $wg_lng;
                        $casm_y2 = $wg_lat;
                        $x1_x2 = $casm_x2 - $casm_x1;
                        $y1_y2 = $casm_y2 - $casm_y1;
                        $casm_v = sqrt($x1_x2 * $x1_x2 + $y1_y2 * $y1_y2) / $t1_t2;
                        if (casm_v > 3185)
                        {
                            $china_lng = 0;
                            $china_lat = 0;
                            return 1;
                        }

                    }
                    $casm_t1 = $casm_t2;
                    $casm_f = $casm_f + 1;
                    $casm_x1 = $casm_x2;
                    $casm_f = $casm_f + 1;
                    $casm_y1 = $casm_y2;
                    $casm_f = $casm_f + 1;
                }
            }
            $x_add = self::Transform_yj5($x_l - 105, $y_l - 35);
            $y_add = self::Transform_yjy5($x_l - 105, $y_l - 35);
            $h_add = $wg_heit;

            $x_add = $x_add + $h_add * 0.001 + self::yj_sin2($wg_time * 0.0174532925199433) + self::random_yj($casm_rr);
            $y_add = $y_add + $h_add * 0.001 + self::yj_sin2($wg_time * 0.0174532925199433) + self::random_yj($casm_rr);
            $china_lng = round(($x_l + self::Transform_jy5($y_l, $x_add)) * 3686400);
            $china_lat = round(($y_l + self::Transform_jyj5($y_l, $y_add)) * 3686400);
            //int.TryParse(((x_l + Transform_jy5(y_l, x_add)) * 3686400).ToString("##"), out china_lng);
            //int.TryParse(((y_l + Transform_jyj5(y_l, y_add)) * 3686400).ToString("##"), out china_lat);
            return 1;
        }


        public static function ToChina_FillRow( $fromLng, $fromLat, &$toLng, &$toLat)
        {
            //LngLat lngLat = (LngLat)obj;

            //double fromLng = lngLat.Lng;
            //double fromLat = lngLat.Lat;
            if ($fromLng <= 0.00 || $fromLat <= 0.00)
            {
                $toLng = $fromLng;
                $toLat = $fromLat;
            }
            else
            {
                $tLng = 0;
                $tLat = 0;
                //int sLng, sLat;
                $sLng = round($fromLng * 1024 * 3600);
                $sLat = round($fromLat * 1024 * 3600);
                //sLng = int.Parse((fromLng * 1024 * 3600).ToString("##"));
                //sLat = int.Parse((fromLat * 1024 * 3600).ToString("##"));
                self::wgtochina_lb(1, $sLng, $sLat, 0, 0, 0, &$tLng, &$tLat);

                $toLng = $tLng * 1.0 / 1024.0 / 3600.0;
                $toLat = $tLat * 1.0 / 1024.0 / 3600.0;            
                if ($toLng <= 0.00) $toLng = $fromLng;
                if ($toLat <= 0.00) $toLat = $fromLat;
            }
        }
    }
?>